package cn.edu.hzvtc.service.impl;

import cn.edu.hzvtc.dao.AreaMapper;
import cn.edu.hzvtc.dao.UnitMapper;
import cn.edu.hzvtc.dao.UnitTypeMapper;
import cn.edu.hzvtc.pojo.Unit;
import cn.edu.hzvtc.pojo.UnitType;
import cn.edu.hzvtc.pojo.User;
import cn.edu.hzvtc.service.unitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service("unitService")
public class unitServiceImpl implements unitService {

    @Autowired
    private UnitTypeMapper unitTypeMapper;
    @Autowired
    private UnitMapper unitMapper;

    @Override
    public List<UnitType> getUnitTypesByAreaId(Integer areaId) {
        return unitTypeMapper.selectByAreaId(areaId);
    }


    @Override
    public Boolean deleteUnitType(Integer id) {
        Boolean result = false;

        if(unitTypeMapper.delStateByPrimaryKey(id)!=0) {
            result = true;
        }

        return result;
    }


    @Override
    public Integer addUnitType(UnitType unitType) {
        return unitTypeMapper.insert(unitType);
    }


    @Override
    public UnitType getUnitType(Integer id){
        return  unitTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateUnitType(UnitType unitType){
        UnitType unitTypeUpdate=unitTypeMapper.selectByPrimaryKey(unitType.getId());
        unitTypeUpdate.setUnitTypeName(unitType.getUnitTypeName());
        unitTypeUpdate.setUnitTypeSortNum(unitType.getUnitTypeSortNum());
        return unitTypeMapper.updateByPrimaryKey(unitTypeUpdate);
    }



    @Override
    public List<Unit> getUnits(Integer unitTypeId, String unitName, Integer areaId) {
        return unitMapper.selectUnits(unitTypeId,unitName,areaId);
    }

    @Override
    public Boolean deleteUnit(String ids) {
        Boolean result = false;

        if (ids.contains("-")) {
            //批量删除
            String[] str_ids = ids.split("-");
            List<Integer> list_ids = new ArrayList<Integer>();
            for (String str_id : str_ids) {
                list_ids.add(Integer.parseInt(str_id));
            }
            unitMapper.delStatesByList(list_ids);
            result = true;
        } else {
            //单一删除
            Integer id = Integer.parseInt(ids);
            unitMapper.delStateByPrimaryKey(id);
            result = true;
        }
        return result;
    }

    @Override
    public Integer addUnit(Unit unit) {
        return unitMapper.insert(unit);
    }

    @Override
    public Unit getUnit(Integer id) {
        return unitMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateUnit(Unit unit) {
        Unit unitUpdate=unitMapper.selectByPrimaryKey(unit.getId());
        unitUpdate.setUnitName(unit.getUnitName());
        unitUpdate.setUnitSortNum(unit.getUnitSortNum());
        if(unit.getUnitTypeId()!=null)
            unitUpdate.setUnitTypeId(unit.getUnitTypeId());

        return unitMapper.updateByPrimaryKey(unitUpdate);
    }


    @Override
    public Integer addUnits(List<Unit> units) {
        return unitMapper.insertUnits(units);
    }


    @Override
    public List<Unit> getUnitByUserId(Integer userId) {
        return unitMapper.selectByUserId(userId);
    }

    @Override
    public Long getCount(Integer userAreaId) {
        return unitMapper.getCount(userAreaId);
    }

}
